<template>
  <div class="navBarContainer">
    <div class="navBar">
      <div
        class="navBarItem"
        :class="index == activeNum ? 'active' : ''"
        v-for="(item, index) in navBarItem"
        :key="index"
        @click="clickBarItem(item.path, index)"
      >{{item.name}}</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onBeforeMount } from 'vue'
import { useRouter } from "vue-router";
const router = useRouter()
const props = defineProps({
  navBarItem: {
    type: Array,
    default() {
      return []
    },
  },
})

onBeforeMount(() => {
    // 根据地址判断activeItem
    // 当前地址
    const currentPath = router.currentRoute.value.fullPath
    props.navBarItem.forEach((item, index) => {
        if (currentPath.search(item.path) != -1) {
            activeNum.value = index
        }
    })
})

const clickBarItem = (path, index) => {
    if (activeNum.value == index) {
        return
    }
    activeNum.value = index
    router.push('/home'+path)
}

    let activeNum = ref(0)
</script>

<style scoped>
.navBarContainer {
  height: 60px;
  width: 100%;
  position: relative;
}

.navBar {
  display: flex;
  position: relative;
  left: 0;
  top: 0;
  background-color: #fff;
  z-index: 10;
  width: 100%;
  color: rgb(49, 49, 49);
  align-items: center;
  padding: 10px 0 20px 20px;
}

.navBarItem {
  font-size: 16px;
  margin: 0 10px;
  text-align: center;
  cursor: pointer;
}

.active {
  font-size: 20px;
  color: black;
  font-weight: 600;
  padding-bottom: 5px;
  border-bottom: 3px solid #ec4141;
  transform: translateY(4px);
}
</style>
